package com.qf.filter;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * @author Kuke
 * @date 2021/9/6
 *
 * 自动登录的过滤器
 */
        /*配置拦截规则:请求转发拦截,或者直接请求拦截*/
@WebFilter(urlPatterns = "/jsp/login.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
public class AutoUserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("自动登录过滤器初始化了...");

    }

    //过滤任务
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        //将ServletRequest转换成HttpServletRequest
        HttpServletRequest request = (HttpServletRequest) req;
        //获取Cookie
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            //声明变量content
            String content = null ;
            //遍历cookie数组
            for(Cookie cookie: cookies){
                //通过指定的cookie的名称的获取内容
                //autoUser
                if(cookie.getName().equals("autoUser")){
                    //cookie名称一致,通过cookie的获取value
                     content = cookie.getValue();
                }
            }

            //判断content是否为null
            if(content!=null){  //杨桃:e10adc3949ba59abbe56e057f20f883e
                //解密
                content = URLDecoder.decode(content,"utf-8") ;
                //拆分用户名和密码
                String[] strs = content.split(":");
                String username = strs[0] ;
                String password = strs[1] ;

                //调用业务接口,通过用户名和密码找用户
                UserService userService = new UserServiceImpl() ;
                User user = userService.getUser(username, password);
                //判断
                if(user!=null){
                    //获取session: 如果当前web容器有session,直接获取session;没有session就创建session
                    HttpSession session = request.getSession();
                    //将user存储在sesison域中
                    session.setAttribute("user",user);
                    //ServletResponse----HttpServletResponse
                    HttpServletResponse response  = (HttpServletResponse) resp;
                    //重定向
                    response.sendRedirect(request.getContextPath()+"/");
                }else{
                    //放行
                    chain.doFilter(req,resp);
                }

            }else{
                //为null
                chain.doFilter(req,resp);
            }




        }else{
            //为null,放行
            chain.doFilter(req,resp);
        }

    }

    @Override
    public void destroy() {
        System.out.println("自动登录过滤器销毁...");
    }
}
